package kr.saeildamil.androidstudy_second.valueani;

import kr.saeildamil.andrordstudy.second.R;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
import android.view.animation.BounceInterpolator;

public class AnimView extends View {
	Ball mBall;
	Paint mPaint;
	ObjectAnimator mToRight;
	ObjectAnimator mToDown;
	ObjectAnimator mEnLarge;
	
	public AnimView(Context context) {
		super(context);
		init();
	}
	
	public AnimView(Context context, boolean status){
		super(context);
		init();
		mToRight = ObjectAnimator.ofInt(mBall, "x", 100, 460);
		mToRight.setDuration(2000);
		mToRight.addUpdateListener(listener);
		
		mToDown = ObjectAnimator.ofInt(mBall, "y", 50, 300);
		mToDown.setDuration(2000);
		mToDown.addUpdateListener(listener);
		
		mEnLarge = ObjectAnimator.ofInt(mBall, "rad", 20, 50);
		mEnLarge.setDuration(2000);
		mEnLarge.addUpdateListener(listener);
	}
	
	AnimatorUpdateListener listener = new AnimatorUpdateListener() {
		public void onAnimationUpdate(ValueAnimator animation) {
			invalidate();
		}
	};
	
	private void init(){
		mBall = new Ball();
		mBall.setX(100);
		mBall.setY(50);
		mBall.setRad(20);
		
		mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		mPaint.setColor(Color.BLUE);
	}
	
	public void startAnim(int id){
		mBall.setX(100);
		mBall.setY(50);
		mBall.setRad(20);
		
		AnimatorSet set = new AnimatorSet();
		switch(id){
		case R.id.btn1:
			set.playSequentially(mToRight, mToDown, mEnLarge);
			break;
		case R.id.btn2:
			set.playSequentially(mToDown, mToRight,  mEnLarge);
			break;
		case R.id.btn3:
			set.playTogether(mToRight, mToDown, mEnLarge);
			break;
		case R.id.btn4:
			set.play(mToRight).with(mEnLarge).before(mToDown);
			break;
		}
		set.start();
	}
	
	public void startAnim(){
		ObjectAnimator anim = ObjectAnimator.ofInt(mBall, "x", 100, 460);
		anim.setDuration(2000);
		anim.setInterpolator(new BounceInterpolator());
		anim.addUpdateListener(new AnimatorUpdateListener() {
			public void onAnimationUpdate(ValueAnimator animation) {
				invalidate();
			}
		});
		anim.start();
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.drawCircle(mBall.getX(), mBall.getY(), mBall.getRad(), mPaint);
	}
}
